package cs3343.project.family.UI;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.Card;
import cs3343.project.family.Character;
import cs3343.project.family.GameController;
import cs3343.project.family.UI.ToolCardFrame;

public class EventCardFrame extends JFrame implements ActionListener {

	private JPanel contentPane;
	private GameController gc;
	
	private JPanel descriptionPanel;
	private JPanel cardPanel;
	private JButton okay;
	
	private JLabel description;
	private JButton btn_A;
	private JButton btn_B;
	private JButton btn_C;
	private JButton btn_D;
	private JLabel msg;
	
	private String[] option;
	private JComboBox<String> availableCard;
	
	private Character currentPlayer;
	private Card currentCard;

	/**
	 * Create the frame.
	 */
	public EventCardFrame(Character player, Card card, GameController gc) {
		this.gc = gc;
		initUI();
		
		currentPlayer = player;
		currentCard = card;
		
		descriptionPanel = new JPanel();
		cardPanel = new JPanel();
		okay = new JButton("OK");
		description = new JLabel(card.getDescription());
		
		btn_A = new JButton(currentCard.getOption(0));
		btn_B = new JButton(currentCard.getOption(1));
		btn_C = new JButton(currentCard.getOption(2));
		btn_D = new JButton(currentCard.getOption(3));
		
		msg = new JLabel(" ");
		
		option = ToolCardFrame.availableCards(currentPlayer, "EventCard");
		availableCard = new JComboBox<String>(option);
		
		Font f = new Font("Dialog", Font.PLAIN, 30);
	    msg.setFont(f);
	    
	    descriptionPanel.setLayout(new BorderLayout());
		cardPanel.setLayout(new GridLayout(3, 2));
		
		add(cardPanel, BorderLayout.SOUTH);
		add(descriptionPanel, BorderLayout.NORTH);

		descriptionPanel.add(description, BorderLayout.NORTH);
		descriptionPanel.add(availableCard, BorderLayout.SOUTH);
		cardPanel.add(btn_A);
		cardPanel.add(btn_B);
		cardPanel.add(btn_C);
		cardPanel.add(btn_D);
		cardPanel.add(msg);
		
		cardPanel.add(okay);
		okay.setEnabled(false);
		
		btn_A.addActionListener(this);
		btn_B.addActionListener(this);
		btn_C.addActionListener(this);
		btn_D.addActionListener(this);
		okay.addActionListener(this);
		//contentPane.add(cardPanel);
	}
	
	private void initUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn_A) {
			btn_A.setEnabled(false);
			btn_B.setVisible(false);
			btn_C.setVisible(false);
			btn_D.setVisible(false);
        	okay.setEnabled(true);
        	msg.setText("Your Self-Esteem: " + currentCard.getResult(0));

        	int oriEsteem = currentPlayer.getEsteem();
        	currentPlayer.setEsteem(oriEsteem += Integer.parseInt(currentCard.getResult(0)));
        	for (int i = 0; i < gc.getCharacterController().getNoOfPlayers(); i++) {
        		gc.getUiController().getGameFrame().setPlayerEsteemLabel(i);
        	}
		}
		else if (e.getSource() == btn_B) {
	    	//cardimg.setVisible(true);
	    	btn_B.setEnabled(false);
	    	btn_A.setVisible(false);
	    	btn_C.setVisible(false);
	    	btn_D.setVisible(false);
	    	okay.setEnabled(true);
	    	msg.setText("Your Self-Esteem: " + currentCard.getResult(1));

	    	int oriEsteem = currentPlayer.getEsteem();
	    	currentPlayer.setEsteem(oriEsteem += Integer.parseInt(currentCard.getResult(1)));
	    	for (int i = 0; i < gc.getCharacterController().getNoOfPlayers(); i++) {
	    		gc.getUiController().getGameFrame().setPlayerEsteemLabel(i);
	    	}
		}
		else if (e.getSource() == btn_C) {
			btn_C.setEnabled(false);
			btn_A.setVisible(false);
			btn_B.setVisible(false);
			btn_D.setVisible(false);
        	okay.setEnabled(true);
        	msg.setText("Your Self-Esteem: " + currentCard.getResult(2));
        	int oriEsteem = currentPlayer.getEsteem();
        	currentPlayer.setEsteem(oriEsteem += Integer.parseInt(currentCard.getResult(2)));
        	for (int i = 0; i < gc.getCharacterController().getNoOfPlayers(); i++) {
	    		gc.getUiController().getGameFrame().setPlayerEsteemLabel(i);
	    	}
		}
		else if (e.getSource() == btn_D) {
			btn_D.setEnabled(false);
			btn_A.setVisible(false);
			btn_B.setVisible(false);
			btn_C.setVisible(false);
        	okay.setEnabled(true);
        	msg.setText("Your Self-Esteem: " + currentCard.getResult(3));
        	int oriEsteem = currentPlayer.getEsteem();
        	currentPlayer.setEsteem(oriEsteem += Integer.parseInt(currentCard.getResult(3)));
        	for (int i = 0; i < gc.getCharacterController().getNoOfPlayers(); i++) {
	    		gc.getUiController().getGameFrame().setPlayerEsteemLabel(i);
	    	}
		}
		else if (e.getSource() == okay) {
			this.dispose();
		}
    }

}
